Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The bn.js package is a library that provides support for arbitrary-precision arithmetic operations on big numbers in JavaScript. It is commonly used in cryptography, financial calculations, and anywhere precise arithmetic with large integers is required.
Big Number Arithmetic
Performing basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo on big numbers.
const BN = require('bn.js');
let a = new BN('123456789');
let b = new BN('987654321');
let sum = a.add(b); // Addition
let diff = a.sub(b); // Subtraction
let product = a.mul(b); // Multiplication
let quotient = a.div(b); // Division
let remainder = a.mod(b); // Modulo
Comparison Operations
Comparing big numbers to determine if they are equal, less than, or greater than each other.
const BN = require('bn.js');
let a = new BN('12345');
let b = new BN('12345');
let c = new BN('54321');
a.eq(b); // true, a equals b
a.lt(c); // true, a is less than c
a.gt(c); // false, a is not greater than c
Bitwise Operations
Performing bitwise operations such as AND, OR, XOR, and NOT on big numbers.
const BN = require('bn.js');
let a = new BN('1011', 2);
let b = new BN('1101', 2);
let and = a.and(b); // AND operation
let or = a.or(b); // OR operation
let xor = a.xor(b); // XOR operation
let not = a.not(); // NOT operation
Reduction Contexts
Using reduction contexts to perform modular arithmetic efficiently, particularly useful in modular exponentiation and cryptographic operations.
const BN = require('bn.js');
let p = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16);
let a = new BN('7fffffffffffffffffffffff7ffffe17ff', 16);
let red = BN.red(p);
let b = a.toRed(red); // Convert to Montgomery representation
let c = b.redSqr().redMul(b); // Perform operations in reduction context
let d = c.fromRed(); // Convert out of Montgomery representation
big.js is a small, fast JavaScript library for arbitrary-precision decimal arithmetic. It focuses on decimal numbers and provides a simpler API but does not support bitwise operations like bn.js.
bignumber.js is another arbitrary-precision arithmetic library for JavaScript and Node.js. It supports decimal and non-decimal arithmetic and has a more extensive API than bn.js, but it might be slower for some operations due to its focus on decimal precision.
decimal.js is a library for arbitrary-precision decimal arithmetic. It is similar to bignumber.js in its focus on decimal numbers and provides a comprehensive set of features for decimal arithmetic, but it does not provide the same level of support for non-decimal and bitwise operations as bn.js.
BigNum in pure javascript
npm install --save bn.js
const BN = require('bn.js');
// Numbers
new BN(12345); // <BN: 3039>
new BN(0x4123456); // <BN: 4123456>
// Strings
new BN('FF', 16); // <BN: 255>
new BN('1A6B765D8CDF', 16); // <BN: 29048849665247>
// Big endian
new BN([1,2,3,4]); // <BN: 1020304>
new BN([1,2,3,4]).toArray().join(','); // <BN: 1,2,3,4>
// Little endian
new BN([1,2,3], 10, 'le'); // <BN: 30201>
new BN([1,2,3,4], 10, 'le'); // <BN: 4030201>
// bitLength
new BN(0x123456).bitLength(); // <BN: 21>
new BN('123456789', 16).bitLength(); // <BN: 33>
// zeroBits
new BN('11000', 2).zeroBits(); // 3
// iaddn
new BN(-100).sign; // true
new BN(100).sign; // false
// isubn
new BN(-100).isubn(200) // <BN: -300>
// add
new BN(14).add(new BN(26)); // <BN: 28>
// mul
new BN(0x1001).mul(new BN(0x1234)); // <BN: 1235234>
// div
new BN('-69527932928').div(new BN('16974594')); // <BN: -fff>
// mod
new BN('10').mod(new BN(256)); // <BN: a>
// divRound
new BN(9).divRound(new BN(20)).toString(10); // <BN: 0>
// abs
new BN(0x1001).abs(); // <BN: 4097>
// modn
new BN('10', 16).modn(256); // <BN: 10>
// idivn
new BN('10', 16).idivn(3); // <BN: 5>
// shl
new BN('69527932928').shln(13); // <BN: 2060602000000>
// shrn
new BN('69527932928').shrn(13); // <BN: 818180>
// bincn
new BN(0xffffff).bincn(1); // <BN: 1000001>
// imaskn
new BN('123456789', 16).imaskn(4); // <BN: 9>
// gcd
new BN(-18).gcd(new BN(12)); // <BN: 6>
// iand
(new BN('1', 2)
.iand(new BN('1000000000000000000000000000000000000001', 2))
.toString(2); // '1'
// ior
new BN('1', 2)
.ior(new BN('1000000000000000000000000000000000000000', 2));
// <BN: 1000000000000000000000000000000000000001>
// ixor
new BN('1', 2)
.ixor(new BN('11001100110011001100110011001100', 2));
// <BN: '11001100110011001100110011001101'>
// setn
new BN(0).setn(2, true); // <BN: 100>
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2015.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Big number implementation in pure javascript
The npm package bn.js receives a total of 18,584,308 weekly downloads. As such, bn.js popularity was classified as popular.
We found that bn.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.